﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Xml.Linq;
using HRProject.Code.DTO;
using OfficeOpenXml;
using System.IO;
using System.Security.Policy;
using System.Xml;
using System.Text;
using HRProject.Code.Common;

namespace HRProject.Code.FAO
{
    public class SalaryExporter : BaseExcelExporter
    {
        private string templateFile;
        private string outputFile;

        public SalaryExporter(string templateFile, string outputFile)
        {
            this.templateFile = templateFile;
            this.outputFile = outputFile;
        }

        public MemoryStream Export(BLMonthlyBasicSalaryTable basicSalaryTable)
        {
            File.Copy(templateFile, outputFile);
            excelPackage = new ExcelPackage(new FileInfo(outputFile));
            xlWorkSheet = (ExcelWorksheet)excelPackage.Workbook.Worksheets[1];

            int startRowIndex = 8;
            int serialColIndex = 1;
            int nameColIndex = 2;
            int salaryLevelIndex = 4;
            int salaryLevelSalaryCalcIndex = 5;
            int senorityIndex = 6;
            int senoritySalaryIndex = 7;
            int totalBasicSalaryIndex = 8;

            int rowIndex = startRowIndex;
            int serial = 1;
            bool firstAgent = true;

            xlWorkSheet.Cell(6, 1).Value = "Tháng mm Năm yyyy"
                .Replace("mm", Convert.ToString(basicSalaryTable.Month))
                .Replace("yyyy", Convert.ToString(basicSalaryTable.Year));
            string tick = char.ConvertFromUtf32(252);
            foreach (BLAgentMonthlyBasicSalary agentSalaryInfo in basicSalaryTable.AgentBasicSalaryInfos)
            {
                if (firstAgent)
                {
                    firstAgent = false;
                }
                else
                {
                    xlWorkSheet.InsertRow(rowIndex);
                    xlWorkSheet.Cell(rowIndex, serialColIndex).Style = "c1";
                    xlWorkSheet.Cell(rowIndex, nameColIndex).Style = "c20";
                    xlWorkSheet.Cell(rowIndex, nameColIndex+1).Style = "c21";
                    xlWorkSheet.Cell(rowIndex, salaryLevelIndex).Style = "c1";
                    xlWorkSheet.Cell(rowIndex, salaryLevelSalaryCalcIndex).Style = "c3";
                    xlWorkSheet.Cell(rowIndex, senorityIndex).Style = "c1";
                    xlWorkSheet.Cell(rowIndex, senoritySalaryIndex).Style = "c3";
                    xlWorkSheet.Cell(rowIndex, totalBasicSalaryIndex).Style = "c4";
                }
                xlWorkSheet.Cell(rowIndex, serialColIndex).Value = Convert.ToString(serial++);
                xlWorkSheet.Cell(rowIndex, nameColIndex).Value = agentSalaryInfo.Agent.FullName;
                xlWorkSheet.Cell(rowIndex, salaryLevelIndex).Value = Convert.ToString(agentSalaryInfo.SalaryLevel.InfoNumberValue);
                xlWorkSheet.Cell(rowIndex, salaryLevelSalaryCalcIndex).Value = Convert.ToString(agentSalaryInfo.SalaryLevelSalaryCalc);
                xlWorkSheet.Cell(rowIndex, senorityIndex).Value = Convert.ToString(agentSalaryInfo.Senority.InfoNumberValue);
                xlWorkSheet.Cell(rowIndex, senoritySalaryIndex).Value = Convert.ToString(agentSalaryInfo.SenoritySalaryCalc);
                xlWorkSheet.Cell(rowIndex, totalBasicSalaryIndex).Value = Convert.ToString(agentSalaryInfo.TotalBasicSalary);
                rowIndex++;
            }

            base.Export();

            FileStream fs = new FileStream(outputFile, FileMode.Open);
            byte[] buffer = new byte[300000];
            int count = fs.Read(buffer, 0, 300000);
            Console.Out.WriteLine("read: " + count);
            fs.Close();
            MemoryStream stream = new MemoryStream(buffer, 0, count);
            buffer = null;
            File.Delete(outputFile);
            return stream;
        }

        public MemoryStream Export(BLMonthlyResponsibleSalaryTable salaryTable)
        {
            File.Copy(templateFile, outputFile);
            excelPackage = new ExcelPackage(new FileInfo(outputFile));
            xlWorkSheet = (ExcelWorksheet)excelPackage.Workbook.Worksheets[1];

            int startRowIndex = 8;
            int serialColIndex = 1;
            int nameColIndex = 2;
            int responsibleLevelIndex = 4;
            int responsibleDescriptionIndex = 5;
            int totalResponsibleSalaryIndex = 6;

            int rowIndex = startRowIndex;
            int serial = 1;
            bool firstAgent = true;

            xlWorkSheet.Cell(6, 1).Value = "Tháng mm Năm yyyy"
                .Replace("mm", Convert.ToString(salaryTable.Month))
                .Replace("yyyy", Convert.ToString(salaryTable.Year));
            string tick = char.ConvertFromUtf32(252);
            foreach (BLAgentMonthlyResponsibleSalary agentSalaryInfo in salaryTable.AgentResponsibleSalaryInfos)
            {
                if (firstAgent)
                {
                    firstAgent = false;
                }
                else
                {
                    xlWorkSheet.InsertRow(rowIndex);
                    xlWorkSheet.Cell(rowIndex, serialColIndex).Style = "c1";
                    xlWorkSheet.Cell(rowIndex, nameColIndex).Style = "c20";
                    xlWorkSheet.Cell(rowIndex, nameColIndex + 1).Style = "c21";
                    xlWorkSheet.Cell(rowIndex, responsibleLevelIndex).Style = "c3";
                    xlWorkSheet.Cell(rowIndex, responsibleDescriptionIndex).Style = "c4";
                    xlWorkSheet.Cell(rowIndex, totalResponsibleSalaryIndex).Style = "c5";
                }
                xlWorkSheet.Cell(rowIndex, serialColIndex).Value = Convert.ToString(serial++);
                xlWorkSheet.Cell(rowIndex, nameColIndex).Value = agentSalaryInfo.Agent.FullName;
                xlWorkSheet.Cell(rowIndex, responsibleLevelIndex).Value = Convert.ToString(agentSalaryInfo.ResponsibleLevel.InfoNumberValue);
                xlWorkSheet.Cell(rowIndex, responsibleDescriptionIndex).Value = agentSalaryInfo.ResponsibleLevel.Description;
                xlWorkSheet.Cell(rowIndex, totalResponsibleSalaryIndex).Value = Convert.ToString(agentSalaryInfo.TotalResponsibleSalary);
                rowIndex++;
            }

            base.Export();

            FileStream fs = new FileStream(outputFile, FileMode.Open);
            byte[] buffer = new byte[300000];
            int count = fs.Read(buffer, 0, 300000);
            Console.Out.WriteLine("read: " + count);
            fs.Close();
            MemoryStream stream = new MemoryStream(buffer, 0, count);
            buffer = null;
            File.Delete(outputFile);
            return stream;
        }

        public MemoryStream Export(BLMonthlyHouseAllowanceTable salaryTable)
        {
            File.Copy(templateFile, outputFile);
            excelPackage = new ExcelPackage(new FileInfo(outputFile));
            xlWorkSheet = (ExcelWorksheet)excelPackage.Workbook.Worksheets[1];

            int startRowIndex = 8;
            int serialColIndex = 1;
            int nameColIndex = 2;
            int houseAllowanceIndex = 4;

            int rowIndex = startRowIndex;
            int serial = 1;
            bool firstAgent = true;

            xlWorkSheet.Cell(6, 1).Value = "Tháng mm Năm yyyy"
                .Replace("mm", Convert.ToString(salaryTable.Month))
                .Replace("yyyy", Convert.ToString(salaryTable.Year));
            string tick = char.ConvertFromUtf32(252);
            foreach (BLAgentMonthlyHouseAllowance agentSalaryInfo in salaryTable.AgentHouseAllowanceInfos)
            {
                if (firstAgent)
                {
                    firstAgent = false;
                }
                else
                {
                    xlWorkSheet.InsertRow(rowIndex);
                    xlWorkSheet.Cell(rowIndex, serialColIndex).Style = "c1";
                    xlWorkSheet.Cell(rowIndex, nameColIndex).Style = "c20";
                    xlWorkSheet.Cell(rowIndex, nameColIndex + 1).Style = "c21";
                    xlWorkSheet.Cell(rowIndex, houseAllowanceIndex).Style = "c3";
                }
                xlWorkSheet.Cell(rowIndex, serialColIndex).Value = Convert.ToString(serial++);
                xlWorkSheet.Cell(rowIndex, nameColIndex).Value = agentSalaryInfo.Agent.FullName;
                xlWorkSheet.Cell(rowIndex, houseAllowanceIndex).Value = Convert.ToString(agentSalaryInfo.HouseAllowance.InfoNumberValue);
                rowIndex++;
            }

            base.Export();

            FileStream fs = new FileStream(outputFile, FileMode.Open);
            byte[] buffer = new byte[300000];
            int count = fs.Read(buffer, 0, 300000);
            Console.Out.WriteLine("read: " + count);
            fs.Close();
            MemoryStream stream = new MemoryStream(buffer, 0, count);
            buffer = null;
            File.Delete(outputFile);
            return stream;
        }
    }
}
